//
//  ClockView.m
//  ClockViewTest
//
//  Created by chenheren on 16/5/11.
//  Copyright © 2016年 HR. All rights reserved.
//

#import "ClockView.h"

#define WIDTH 14.0f  //刻度线条长度
#define SPACE M_PI/40  //刻度间隔
#define DEGREEWIDTH M_PI/130   //刻度宽度
#define EDGESET 30.0f  //表盘边缘

@interface ClockView ()

@property (nonatomic)float lowTmp;
@property (nonatomic)float highTmp;
@property (nonatomic)float currentTmp;
@end

@implementation ClockView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.


-(instancetype)initWithFrame:(CGRect)frame
{
    //重写initWithFrame必须先调用父类的方法
    self=[super initWithFrame:frame];
    if (self) {
        self.currentTmp=0;
        self.lowTmp=0;
        self.highTmp=0;
        self.backgroundColor=[UIColor colorWithRed:1.000 green:0.992 blue:0.388 alpha:1.000];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, WIDTH);
    
    
    float startAngle=M_PI*2/3;
    float endAngle=M_PI*7/3;
    float radius=self.frame.size.height > self.frame.size.width ? self.frame.size.width : self.frame.size.height;
    radius =radius/2-EDGESET;
    
    
    float lowAngle=(M_PI/36) * (self.lowTmp +20)+startAngle;
    float highAngle=(M_PI/36) * (self.highTmp +20)+startAngle;
    
    //开始刻度
    CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 0.8);
    CGContextSetLineWidth(ctx, WIDTH+10);
    CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, radius, startAngle, startAngle+M_PI/120, 0);
    CGContextStrokePath(ctx);
    
    //中间刻度
    CGContextSetLineWidth(ctx, WIDTH);
    for (float angle=startAngle; angle<endAngle; angle+=SPACE) {
        if (angle>=lowAngle && angle<=highAngle) {
            CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1);
        }else{
                CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 0.8);  //设置画笔颜色
        }
        CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, radius, angle, angle+DEGREEWIDTH, 0);
        CGContextStrokePath(ctx);
    }
    
    //结束刻度
    CGContextSetLineWidth(ctx, WIDTH+10);
    CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, radius, endAngle, endAngle+M_PI/120, 0);
    CGContextStrokePath(ctx);
    
    //最低温度字体
    float lowTmpX=(radius+10)*cos(-lowAngle)+self.frame.size.width/2;
    float lowTmpY=-(radius+10)*sin(-lowAngle)+self.frame.size.height/2;
    NSDictionary *lowAttr=@{@"UIFont":[UIFont boldSystemFontOfSize:8.0]};
    [[NSString stringWithFormat:@"%g°",self.lowTmp]drawInRect:CGRectMake(lowTmpX, lowTmpY, 50, 20) withAttributes:lowAttr];
    
    //最低温度字体
    float highTmpX=(radius+10)*cos(-highAngle)+self.frame.size.width/2;
    float highTmpY=-(radius+10)*sin(-highAngle)+self.frame.size.height/2;
    NSDictionary *highAttr=@{@"UIFont":[UIFont boldSystemFontOfSize:8.0]};
    [[NSString stringWithFormat:@"%g°",self.highTmp]drawInRect:CGRectMake(highTmpX, highTmpY, 50, 20) withAttributes:highAttr];
    
    
    //开始刻度字体
    float startX=(radius+10) * cos(-startAngle)+self.frame.size.width/2;
    float startY=-(radius +10)* sin(-startAngle)+self.frame.size.height/2;
    NSLog(@"%lf,%lf,%lf",radius , startX,startY);
    NSDictionary *startAttr=@{@"UIFont":[UIFont boldSystemFontOfSize:10.0f]};
    [@"-20℃" drawInRect:CGRectMake(startX-35, startY, 80, 20) withAttributes:startAttr];
    
    
    //结束刻度字体
    float endX=(radius+10) * cos(-endAngle)+self.frame.size.width/2;
    float endY=-(radius +10)* sin(-endAngle)+self.frame.size.height/2;
    NSLog(@"%lf,%lf,%lf",radius , startX,startY);
    NSDictionary *endAttr=@{@"UIFont":[UIFont boldSystemFontOfSize:10.0f]};
    [@"40℃" drawInRect:CGRectMake(endX, endY, 80, 20) withAttributes:endAttr];
    
    //画指针
    CGContextSetLineWidth(ctx, 4.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
    CGContextMoveToPoint(ctx, self.frame.size.width/2, self.frame.size.height/2);
    
    float currentAngle=(M_PI/36) * (self.currentTmp +20)+startAngle;
    float x = (radius-10) * cos(-currentAngle) + self.frame.size.width/2;
    float y =-(radius -10) * sin(-currentAngle) + self.frame.size.height/2;
    NSLog(@"%lf,%lf,%lf",currentAngle,x,y);
    CGContextAddLineToPoint(ctx, x, y);
    CGContextStrokePath(ctx);
    
    //表盘中心点
    CGContextSetLineWidth(ctx, 2.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
    CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, 5, 0, M_PI*2, 0);
    [[UIColor blueColor] set];
    CGContextFillPath(ctx);
}


-(void)setCurrentTemperature:(float)currentTmp withLowTemp:(float)lowTmp withHigh:(float)highTmp;
{
    self.currentTmp=currentTmp;
    self.lowTmp=lowTmp;
    self.highTmp=highTmp;
    [self setNeedsDisplay];
}


@end
